/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao.erp;

import e.odbo.data.dao.ViewDAO;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataParams;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.serialize.serializer.SerializerRead;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteParams;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;
import com.openbravo.bean.erp.viewbean.OrderLineConsultInfo;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class OrderLineConsultInfoDAO extends ViewDAO<OrderLineConsultInfo>{

    public OrderLineConsultInfoDAO(I_Session s) {
        super(s);
    }

    @Override
    public Class getSuportClass() {
        return OrderLineConsultInfo.class;
    }

    public OrderLineConsultInfo readValues(DataRead dr,OrderLineConsultInfo p) throws BasicException {
       if(p==null)
    	   p=new OrderLineConsultInfo();
       p.productID=dr.getString(1);
       p.attributesetid=dr.getString(2);
       p.multiply=dr.getDouble(3);
       return p;
    }


    public List<OrderLineConsultInfo> listConsultInfoByOrder(String orderId) throws BasicException {
        return new PreparedSentence(s, "SELECT PRODUCT, ATTRIBUTESETINSTANCE_ID, UNITS FROM ORDERLINES WHERE  ORDERRID= ?  ", SerializerWriteString.INSTANCE, this).list(orderId);
    }


    public List<OrderLineConsultInfo> listConsultInfoByRealStore(String locationId) throws BasicException {
        return new PreparedSentence(s, "SELECT PRODUCT, ATTRIBUTESETINSTANCE_ID, UNITS FROM STOCKCURRENT WHERE  LOCATION= ?  ", SerializerWriteString.INSTANCE, this).list(locationId);
    }


    public List<OrderLineConsultInfo> listConsultInfoByTimeStore(String timeStoreId) throws BasicException {
        return new PreparedSentence(s, "SELECT PRODUCT, ATTRIBUTESETINSTANCE_ID, UNITS FROM TIMESTORELINES  WHERE  TIMESTOREID= ?  ", SerializerWriteString.INSTANCE, this).list(timeStoreId);
    }


    public List<OrderLineConsultInfo> listConsultInfoByRealStore(final String locationRef, final String customerId) throws BasicException {
        return new PreparedSentence(s, "SELECT S.PRODUCT, S.ATTRIBUTESETINSTANCE_ID, S.UNITS FROM STOCKCURRENT S, ORDERLINES L,ORDERS O WHERE S.PRODUCT=L.PRODUCT AND S.ATTRIBUTESETINSTANCE_ID=L.ATTRIBUTESETINSTANCE_ID  AND L.ORDERRID=O.ID AND S.LOCATION=? AND O.CUSTOMER=? ", SerializerWriteParams.INSTANCE, this).list(new DataParams() {

            public void writeValues() throws BasicException {
                setString(1, locationRef);
                setString(2, customerId);
            }
        });
    }

    public Double getProductUnitsInOrder(final String orderId, final String productId, final String attsetinst) throws BasicException {
    	if(attsetinst==null)
        	return (Double) new PreparedSentence(s, "SELECT L.UNITS FROM ORDERLINES L WHERE  L.ORDERRID = ? AND L.PRODUCT=? AND L.ATTRIBUTESETINSTANCE_ID IS NULL ", SerializerWriteParams.INSTANCE, new SerializerRead() {

                @Override
                public Object readValues(DataRead dr) throws BasicException {
                    return dr.getDouble(1);
                }
            }).find(new DataParams() {

                public void writeValues() throws BasicException {
                    setString(1, orderId);
                    setString(2, productId);
                }
            });
    	return (Double) new PreparedSentence(s, "SELECT L.UNITS FROM ORDERLINES L WHERE  L.ORDERRID = ? AND L.PRODUCT=? AND L.ATTRIBUTESETINSTANCE_ID=? ", SerializerWriteParams.INSTANCE, new SerializerRead() {

            @Override
            public Object readValues(DataRead dr) throws BasicException {
                return dr.getDouble(1);
            }
        }).find(new DataParams() {

            public void writeValues() throws BasicException {
                setString(1, orderId);
                setString(2, productId);
                setString(3, attsetinst);
            }
        });
    }


    public Double getProductUnitsInTimeStore(final String timeStoreId, final String productId, final String attsetinst) throws BasicException {
    	 if(attsetinst==null)
    		 return (Double) new PreparedSentence(s, "SELECT T.UNITS FROM TIMESTORELINES T WHERE  T.TIMESTOREID = ? AND T.PRODUCT=? AND T.ATTRIBUTESETINSTANCE_ID IS NULL ", SerializerWriteParams.INSTANCE, new SerializerRead() {
    	            @Override
    	            public Object readValues(DataRead dr) throws BasicException {
    	                return dr.getDouble(1);
    	            }
    	        }).find(new DataParams() {
    	            public void writeValues() throws BasicException {
    	                setString(1, timeStoreId);
    	                setString(2, productId);
    	            }
    	        });
    	return (Double) new PreparedSentence(s, "SELECT T.UNITS FROM TIMESTORELINES T WHERE  T.TIMESTOREID = ? AND T.PRODUCT=? AND T.ATTRIBUTESETINSTANCE_ID=? ", SerializerWriteParams.INSTANCE, new SerializerRead() {
            @Override
            public Object readValues(DataRead dr) throws BasicException {
                return dr.getDouble(1);
            }
        }).find(new DataParams() {
            public void writeValues() throws BasicException {
                setString(1, timeStoreId);
                setString(2, productId);
                setString(3, attsetinst);
            }
        });
    }


    public Double getProductUnitsInRealStore(final String location, final String productId, final String attsetinst) throws BasicException {
        if(attsetinst==null)
        	return (Double) new PreparedSentence(s, "SELECT T.UNITS FROM STOCKCURRENT T WHERE  T.LOCATION= ? AND T.PRODUCT=? AND T.ATTRIBUTESETINSTANCE_ID IS NULL", SerializerWriteParams.INSTANCE, new SerializerRead() {

                @Override
                public Object readValues(DataRead dr) throws BasicException {
                    return dr.getDouble(1);
                }
            }).find(new DataParams() {
                public void writeValues() throws BasicException {
                    setString(1, location);
                    setString(2, productId);
                }
            });
    	return (Double) new PreparedSentence(s, "SELECT T.UNITS FROM STOCKCURRENT T WHERE  T.LOCATION= ? AND T.PRODUCT=? AND T.ATTRIBUTESETINSTANCE_ID=? ", SerializerWriteParams.INSTANCE, new SerializerRead() {

            @Override
            public Object readValues(DataRead dr) throws BasicException {
                return dr.getDouble(1);
            }
        }).find(new DataParams() {
            public void writeValues() throws BasicException {
                setString(1, location);
                setString(2, productId);
                setString(3, attsetinst);
            }
        });
    }

}
